import Base from '../Base'
import api from '@/server/api'
import moment from 'moment'
import { getUserInfo } from '@/utils'
const { getArticles, getArticleById, createArticle, updateArticle, delArticle } = api
const Md = require('markdown-it')()

export default class extends Base {
  getArticleList () {
    getArticles().then(res => {
      let list = res.result.list
      console.log(list)
      this.vm.articleList = list.map(val => {
        let todayStart = new Date()
        todayStart.setHours(0, 0, 0, 0)
        let time = val.time * 1
        return {
          id: val.articleId,
          articleType: val.articleType,
          title: val.title,
          author: val.username,
          content: val.content,
          type: val.articleType,
          date: time > todayStart ? moment(time).format('HH:MM:SS') : moment(time).format('YYYY-MM-DD'),
          viewed: val.viewed
        }
      })
    })
  }
  // 删除文章
  delArticle (id) {
    if (!id) {
      this.vm.$Message.error('what\'s id ?')
    } else {
      delArticle(id).then(res => {
        if (res.status === 1) {
          this.vm.$Message.error(res.msg)
        } else {
          this.getArticleList()
          this.vm.$Message.success(res.msg)
        }
      })
    }
  }
  // 获取文章详细信息
  getArticle () {
    let id = this.vm.$route.params.articleId
    if (!id) {
      this.vm.$Message.error('article id error')
      return
    }
    getArticleById(id).then(res => {
      let article = res.result.list[0]
      Object.assign(this.vm.param, article, {time: moment(article.time * 1).format('YYYY-MM-DD')})
    })
  }
  convert (md) {
    return Md.render(md)
  }
  save (articleType) {
    const author = getUserInfo().username
    if (!author) {
      this.vm.$Message.error('未登录？跳转到登录。不会保存草稿')
      this.vm.$router.push('/login')
    }
    const param = this.vm.$store.state.article.editing
    if (param.title.trim() === '') {
      this.vm.$Message.warning('必须设置title')
      return false
    } else if (param.content.trim() === '') {
      this.vm.$Message.warning('必须设置content')
      return false
    }
    let article = {
      author,
      content: param.content,
      time: Date.now(),
      title: param.title,
      articleType
    }
    if (this.vm.$route.params.articleId) {
      article.id = this.vm.$route.params.articleId
      updateArticle(article).then(res => {
        this.vm.$Message.success(res.msg)
        this.vm.$router.push('/home')
      })
    } else {
      createArticle(article).then(res => {
        this.vm.$Message.success(res.msg)
        this.vm.$router.push('/home')
      })
    }
  }
  // 跳转到编辑
  edit (articleId) {
    const content = this.vm.content
    this.vm.$store.commit('SET_ID', articleId)
    this.vm.$router.push({name: 'createArticle', params: {content, articleId}})
  }
  // 在内页删除文章 --- 此函数晚点删掉， 用上面到删除替代
  deleteArticle (articleId) {
    articleId = articleId || this.vm.param.articleId
    let vm = this.vm
    delArticle(articleId).then(res => {
      if (res.status === 0) {
        vm.$Message.success('delete success')
        vm.$router.push('/home')
      } else {
        vm.$Message.error('delete fail')
      }
    })
  }
}
